<template>
  <div class="yl-container">
    <div class="yl-main">
      <!-- 搜索模块 -->
      <div class="yl-search-list">
        <el-form :model="searchConfig" ref="searchConfig" :inline="true">
          <el-form-item>
            <el-input  v-model="searchConfig.name" placeholder="请输入类型名称" type="text" size="small"></el-input>
          </el-form-item>
          <el-form-item>
              <YlButton uiType="default" icon="el-icon-search" @click="initData(false)">搜 索</YlButton>
            <YlButton uiType="default" btnType="success" icon="iconfont yl-ecs-zhongzhi yl-icon" @click="initTable(false)">重 置</YlButton>
          </el-form-item>
        </el-form>

        <YlButton uiType="plain" icon="el-icon-circle-plus-outline" @click="initAdd">新增</YlButton>

      </div>
      <br>

      <!-- table 列表组 -->
      <div class="yl-table-list">
        <el-table
          v-loading="loading"
          element-loading-text="玩命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.8)"
          border
          height="550"
          :data="tableData"
        >
          <el-table-column align="center" label="序号" width="80">
            <template slot-scope="scope">
              <span>{{ scope.$index + 1 }}</span>
            </template>
          </el-table-column>

          <el-table-column label="名称" align="center" prop="nns_name"></el-table-column>

          <el-table-column align="center" label="创建时间" prop="nns_create_time" show-overflow-tooltip></el-table-column>

          <el-table-column align="center" label="修改时间" prop="nns_modify_time" show-overflow-tooltip></el-table-column>

          <el-table-column align="center"  label="操作" fixed="right">
            <template slot-scope="scope">
              <YlButton uiType="main" icon="el-icon-view" v-show="query.canWrite" @click="handleShowSingle(scope.row)">编辑</YlButton>
              <YlButton uiType="main" icon="el-icon-delete" btnType="danger" v-show="query.canWrite" @click="del(scope.row)">删除</YlButton>
            </template>
          </el-table-column>
          <div class="yl-table-empty" slot="empty">
            <img src="~@/assets/icons/svg/no-data.svg">
            <div>暂无数据</div>
          </div>
        </el-table>
        <!-- 分页模块 -->
        <YlPage :size="size" :page="page" :total="total" />
      </div>
    </div>
    <el-dialog class="yl-dialog" :title="formConfig.isFix ? '修改类型' : '新增类型'" :visible.sync="dialogVisible">
      <YlForm
        ref="ylform"
        :formConfig="formConfig"
        @close-dialog="dialogVisible = false"
        @init-state="getInitState"
      />
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      dialogVisible: false,
      formConfig: {
        btnMethod: ['submit', 'cancel'],
        inline: false,
        changeId: '',
        isFix: false,
        formData: {
          name: '',
          stage_id: ''
        },
        editRules: {
          name: [
            { required: true, message: '请输入类型名称', trigger: 'blur' }
          ]
        },
        columns: [
          {
            type: 'input',
            label: '类型名称',
            prop: 'name'
          }
        ],
        addApi: this.$ylapi.addNewCategoryOffBudget,
        changeApi: this.$ylapi.changeCategoryOffBudget
      },
      loading: false,
      // table 列表数据
      tableData: [],
      searchConfig: {
        name: ''
      },
      // 分页
      page: 1,
      size: 10,
      total: 0,
      // query信息
      query: {}
    }
  },
  created () {
    this.query = this.$route.query
    this.formConfig.formData.stage_id = this.query.id
    this.initTable()
  },
  methods: {
    // 列表初始化
    initTable () {
      this.dialogVisible = false
      this.searchConfig.name = ''
      this.initData()
    },

    initAdd () {
      this.formConfig.formData = this.initForm(this.formConfig.formData, ['stage_id'])
      this.dialogVisible = true
      this.formConfig.isFix = false
      this.formConfig.btnMethod = ['submit', 'cancel']
    },

    // 数据初始化
    async initData () {
      this.loading = true
      let data = await this.$ylapi.getContentListCategoryOffBuddget({
        'page': this.page,
        'page_size': this.size,
        'stage_id': this.query.id,
        'name': this.searchConfig.name
      })
      this.page = data.page
      this.total = data.total
      this.tableData = data.list
      this.loading = false
      this.formConfig.formData.name = ''
    },

    // 编辑回显
    handleShowSingle ({ nns_id, nns_name }) {
      this.formConfig.changeId = nns_id
      this.formConfig.formData.name = nns_name
      this.formConfig.isFix = true
      this.dialogVisible = true
      this.formConfig.btnMethod = ['save', 'cancel']
    },

    // 删除
    async del ({ nns_id }) {
      // eslint-disable-next-line no-unused-expressions
      await this.$modal.confirm(this, {
        api: this.$ylapi.deleteCategoryOffBudget,
        obj: nns_id
      })
        ? this.initTable()
        : ''
    },

    // 跳转
    goAddCategory (canWrite, id) {
      this.$router.push({
        path: `/addFill`,
        query: {
          id,
          canWrite
        }
      })
    },
    // 分页---页容量选择器
    handlePageSize (val) {
      this.size = val
      this.loading = true
      this.initData()
    },
    // 分页
    handleCurrentPage (val) {
      this.page = val
      this.loading = true
      this.initData()
    },
    // 初始化
    getInitState (val) {
      if (val) {
        this.initTable()
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
